package com.zhuanzhuan.hero.user.facade;

import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZHeader;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZMethod;
import com.bj58.zhuanzhuan.zzentry.common.service.model.openEntry.ZZParam;
import com.zhuanzhuan.hero.user.dto.*;
import com.zhuanzhuan.hero.user.entity.HeroUserPrivacySetup;
import com.zhuanzhuan.hero.user.enums.PrivacyTypeEnum;
import com.zhuanzhuan.hero.user.pojo.bo.QueryUserInfoParam;
import com.zhuanzhuan.hero.user.pojo.po.UserPO;
import com.zhuanzhuan.hero.user.service.HeroUserInfoService;
import com.zhuanzhuan.hero.user.service.HeroUserPaymentService;
import com.zhuanzhuan.hero.user.service.HeroUserPrivacySetupService;
import com.zhuanzhuan.hero.user.service.IUserService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import com.zhuanzhuan.hero.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName UserInfoFacade
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/18 14:55
 */
@ServiceBehavior
@Slf4j
public class UserInfoFacadeImpl implements IUserInfoFacade {

    @Resource
    private IUserService userService;

    @Resource
    private HeroUserInfoService userInfoService;

    @Resource
    private HeroUserPrivacySetupService userPrivacySetupService;

    @Resource
    private HeroUserPaymentService userPaymentService;

    @Override
    public Result<UserBaseInfoVO> queryUserBaseInfo(QueryUserInfoDTO queryUserInfoDTO) {
        UserBaseInfoVO vo = userService.queryUserBaseInfoSelective(queryUserInfoDTO);
        //可能会查询不到返回null  特殊处理一下
        if (Objects.isNull(vo)) {
            return Result.success();
        }
        return Result.success(vo);
    }

    @Override
    public Result<List<Long>> queryUidByContactMobile(String contactMobile) {
        QueryUserInfoParam param = new QueryUserInfoParam();
        param.setContactMobile(contactMobile);
        List<UserPO> userInfos = userInfoService.listByParam(param);
        if (CollectionUtils.isEmpty(userInfos)) {
            return Result.success(new ArrayList<>());
        }

        List<Long> uidList = userInfos.stream().map(UserPO::getUid).collect(Collectors.toList());
        return Result.success(uidList);
    }

    @Override
    public Result<List<UserBaseInfoVO>> batchQueryUserBaseInfo(@Valid BatchQueryUserInfoSelectiveDTO batchQueryUserInfoSelectiveDTO) {
        return Result.success(userService.batchQueryUserBaseInfo(batchQueryUserInfoSelectiveDTO));
    }

    @Override
    public Result<List<UserDetailDTO>> batchGetUserDetail(@Valid BatchQueryUserInfoDTO batchQueryUserInfoDTO,
        ServiceHeaderEntity header) {
        return Result.success(userService.batchGetUserDetail(batchQueryUserInfoDTO.getSelfUid(),
            batchQueryUserInfoDTO.getQueryUids(), header));
    }

    @Override
    public Result<List<UserDetailVO>> batchGetUserDetail4Homepage(@NotNull BatchQueryUserInfoDTO batchQueryUserInfoDTO,
        @NotNull ServiceHeaderEntity header) {
        return Result.success(userService.batchGetUserDetail4Homepage(batchQueryUserInfoDTO.getSelfUid(),
            batchQueryUserInfoDTO.getQueryUids(), header));
    }

    @ZZMethod(value = "queryOuterInfo", desc = "查询回收商端外头像昵称", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserBriefInfoVO> queryOuterInfo(@ZZHeader ServiceHeaderEntity header) {
        return ZZOpenScfBaseResult.buildSucc(userService.queryOuterInfo(ServiceHeaderUtil.parseUid(header)));
    }

    @ZZMethod(value = "saveOuterInfo", desc = "保存回收商端外头像昵称", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> saveOuterInfo(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid SaveOuterInfoDTO saveOuterInfoDTO) {
        Long uid = ServiceHeaderUtil.parseUid(header);
        userInfoService.checkCanEdit(uid);
        userService.updateOuterInfo(uid, saveOuterInfoDTO, header);
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @Override
    public Result<SetupPrivacyVO> queryUserSetupPrivacy(Long uid) {
        SetupPrivacyVO vo = new SetupPrivacyVO();
        List<HeroUserPrivacySetup> setups = userPrivacySetupService.listByUidAndPrivacyType(uid, null);
        if (CollectionUtils.isEmpty(setups)) {
            return Result.success(vo);
        }

        List<Long> invisibleList =
            setups.stream().filter(setup -> PrivacyTypeEnum.INVISIBLE.getCode().equals(setup.getPrivacyType()))
                .map(HeroUserPrivacySetup::getToUid).collect(Collectors.toList());
        vo.setInvisibleUids(invisibleList);

        List<Long> blockList =
            setups.stream().filter(setup -> PrivacyTypeEnum.BLOCK.getCode().equals(setup.getPrivacyType()))
                .map(HeroUserPrivacySetup::getToUid).collect(Collectors.toList());
        vo.setInvisibleUids(blockList);

        return Result.success(vo);
    }

    @Override
    public Result<UserPaymentVO> queryUserDefaultPayment(@NotNull Long uid) {
        return Result.success(userPaymentService.queryUserDefaultPaymentInfo(uid));
    }

    @Override
    public Result<List<UserRealPersonAuthInfoVO>> queryAllRealPersonAuthUserInfo(@NotNull Long uid) {
        return Result.success(userInfoService.queryAllRealPersonAuthUserInfo(uid));
    }

    @ZZMethod(value = "queryUserDetailInfo", desc = "查询用户详情", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserDetailVO> queryUserDetailInfo(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") QueryUserDetailDTO queryUserDetailDTO) {
        return ZZOpenScfBaseResult.buildSucc(userService.queryUserDetailInfo(ServiceHeaderUtil.parseUid(header),
            queryUserDetailDTO.getHomemakerUid(), header));
    }

    @ZZMethod(value = "queryUserDetailInfoOuter", desc = "端外查询用户详情", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserDetailVO> queryUserDetailInfoOuter(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid QueryUserDetailDTO queryUserDetailDTO) {
        return ZZOpenScfBaseResult.buildSucc(userService.queryUserDetailInfoOuter(queryUserDetailDTO.getHomemakerUid()));
    }

    @ZZMethod(value = "queryUserBaseInfo", desc = "查询用户基本信息", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<UserBaseInfoVO> queryUserBaseInfo(@ZZHeader ServiceHeaderEntity header) {
        log.info("k=s act=queryUserBaseInfo uid={}", header.getUid());
        //端外查看【我的】，看的也是端内的头像昵称   这里不区分t值
        UserBaseInfoVO userBaseInfoVO = userService.queryUserBaseInfo(ServiceHeaderUtil.parseUid(header));
        log.info("k=s act=queryUserBaseInfo result={}", JsonUtils.toJsonWithoutNull(userBaseInfoVO));
        return ZZOpenScfBaseResult.buildSucc(userBaseInfoVO);
    }

    @ZZMethod(value = "saveUserBaseInfo", desc = "保存用户基本信息", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> saveUserBaseInfo(@ZZHeader ServiceHeaderEntity header,
        @ZZParam("param") @Valid SaveUserBaseInfoDTO saveUserBaseInfoDTO) {
        log.info("k=s act=saveUserBaseInfo param={}", JsonUtils.toJsonWithoutNull(saveUserBaseInfoDTO));
        //端外修改头像昵称调用该接口，修改的是卖家自己的，和端内通用，不区分t值
        userService.saveUserBaseInfo(ServiceHeaderUtil.parseUid(header), saveUserBaseInfoDTO, header);

        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @ZZMethod(value = "saveUserBackdrop", desc = "保存用户背景图", publicAccess = false)
    @Override
    public ZZOpenScfBaseResult<Boolean> saveUserBackdrop(@ZZHeader ServiceHeaderEntity header, @NotNull String backdrop) {
        userService.saveUserBackdrop(ServiceHeaderUtil.parseUid(header), backdrop, header);
        return ZZOpenScfBaseResult.buildSucc(true);
    }

    @Override
    public Result<Boolean> needHideMemberLevel(@NotNull ServiceHeaderEntity header) {
        return Result.success(userInfoService.needHideMemberLevel(header));
    }

    @Override
    public Result<Boolean> personInfoPerfection(Long uid) {
        return Result.success(userService.personInfoPerfection(uid,null,null,null));
    }
}
